package com.lyw.login.vm

import androidx.lifecycle.viewModelScope
import com.lu.library.base.BaseViewModel
import com.lu.library.base.IUiIntent
import com.lu.library.base.IUiState
import com.lu.library.ext.launchSafe
import com.lu.library.ext.postEvent
import com.lu.library.http.ApiCoroutineExceptionHandler
import com.lu.library.kv.KV
import com.lu.library.kv.KVDelegate
import com.lyw.login.Constants
import com.lyw.login.api.LoginApi
import com.lyw.login.bean.LoginEvent
import com.lyw.login_api.LoginConstants
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

sealed class LoginUiState:IUiState{
    data class INIT(val userName: String,val pwd: String,val loginEnable:Boolean) : LoginUiState()
    data class InfoChange(var loginEnable:Boolean) : LoginUiState()
    data class LoginSuccess(var i:Int) : LoginUiState()
}

sealed class LoginIntent:IUiIntent{
    data class LoginIn(val name: String, val pwd: String) : LoginIntent()
    data class UpdatePwd(val phone:String,val pwd:String, val code:String): LoginIntent()
    data class SendCode(val phone:String): LoginIntent()
    data class UserNameChange(var newName:String):LoginIntent()
    data class PwdChange(var newPwd:String):LoginIntent()
}
@HiltViewModel
class LoginVm  @Inject constructor(
    val kv:KV,
    val api:LoginApi
):BaseViewModel<LoginUiState,LoginIntent>() {
//    private val kv by lazy {
//        MMKVImpl()
//    }
    private var userName by KVDelegate(key = Constants.KEY_USER_NAME,def = "xxx")
    private var pwd by KVDelegate(key = Constants.KEY_USER_PWD,def = "yyy")
    private var tempUserName:String = "xxx"
    private var tempPwd:String = "yyy"
    private var isLogin by KVDelegate(key=LoginConstants.KEY_USER_LOGIN,def=false)
    init {
        updateUiState {
            tempUserName = userName
            tempPwd = pwd
            LoginUiState.INIT(userName,pwd,tempUserName.isNotEmpty()&&tempPwd.isNotEmpty());
        }
    }
    override fun initUiState(): LoginUiState {
        return LoginUiState.INIT("xxx","yyy",false);
    }

    override fun handleIntent(intent: LoginIntent) {
        when(intent){
            is LoginIntent.LoginIn->{
                login(intent)
            }
            is LoginIntent.UserNameChange->{
                tempUserName = intent.newName
                updateUiState {
                    LoginUiState.InfoChange(tempUserName.isNotEmpty()&&tempPwd.isNotEmpty())
                }
            }
            is LoginIntent.PwdChange->{
                tempPwd = intent.newPwd
                updateUiState {
                    LoginUiState.InfoChange(tempUserName.isNotEmpty()&&tempPwd.isNotEmpty())
                }
            }
            else ->{

            }
        }
    }
    var index=0
    private fun login(intent:LoginIntent.LoginIn){
        viewModelScope.launchSafe(Dispatchers.Main+ApiCoroutineExceptionHandler()) {
//            val userInfo = withContext(Dispatchers.IO) {
                userName = intent.name
                pwd = intent.pwd
//                api.login(LoginReq(intent.name, intent.pwd)).data
//            }
//            userInfo?.let {
//                isLogin = true
//                kv.put(LoginConstants.KEY_USER_INFO,GsonUtils.toJson(it))
//            }
//            LogUtils.d(userInfo)
            index++
            updateUiState {
                LoginUiState.LoginSuccess(index/2);
            }
            LoginEvent().postEvent()
        }
    }
}